const {
    createOrder,
    updateOrder,
    removeOrder,
    restoreOrder,
    toPayMoney,
    findOrder,
    findOrderInfo,
    checkPayStatus
} = require('../service/order.service')

const {updateUserMoney} = require("../service/user.service")
const {updateCategory,findCategoryInfo} = require("../service/category.service");

const {sucOperation} = require("../constant/success.type");
const {failOperation} = require("../constant/err.type");
const { v4: uuidv4 } = require('uuid');
const axios = require("axios");
class OrderController {
    async create(ctx, next) {
        try {
            const order_number = uuidv4();
            const user_id = ctx.state.user.id
            const {money,cat_id} = ctx.request.body
            // 1。创建订单
            const { createdAt, updatedAt, ...res } = await createOrder(
                {...ctx.request.body,order_number,user_id}
            );

            // 2.对接支付宝
            const result = await toPayMoney({order_number,money,cat_id});
            // 3.返回支付宝的支付链接
            ctx.body = {
                ...sucOperation,
                result,
            };
        } catch (err) {
            console.log(err)
            return ctx.body = {
                ...failOperation
            };
        }
    }

    async update(ctx) {
        try {
            const res = await updateOrder(ctx.params.id, ctx.request.body);
            console.log(res)
            if (res) {
                ctx.body = {
                    ...sucOperation,
                    result: res,
                };
            } else {
                return ctx.body = {
                    ...failOperation
                };
            }
        } catch (err) {
            console.error(err);
        }
    }

    async remove(ctx) {
        const res = await removeOrder(ctx.params.id);

        if (res) {
            ctx.body = {
                ...sucOperation,
            };
        } else {
            return ctx.body = {
                ...failOperation
            };
        }
    }

    async restore(ctx) {
        const res = await restoreGoods(ctx.params.id);
        if (res) {
            ctx.body = {
                ...sucOperation,
            };
        } else {
            return ctx.body = {
                ...failOperation
            };
        }
    }

    async findAll(ctx) {
        // 1. 解析pageNum和pageSize
        const {
            pageNum = 1,
            pageSize = 10,
            is_user,
            ...params
        } = ctx.request.body;

        if(is_user){
            params.user_id = ctx.state.user.id
        }
        // 2. 调用数据处理的相关方法
        const res = await findOrder({ pageNum, pageSize,...params });
        // 3. 返回结果
        ctx.body = {
            ...sucOperation,
            result: res,
        };
    }

    async findOne(ctx) {
        const { id } = ctx.params;
        const result = await findOrderInfo(id);
        if (result) {
            ctx.body = {
                ...sucOperation,
                result,
            };
        } else {
            ctx.body = {
                code: 500,
                message: "id有误",
            };
        }
    }

    async checkPay(ctx) {
        // 支付完成后检查 订单支付状态
        const url = await checkPayStatus(ctx.request.body)
        const res = await axios({
            url,
            method:"get"
        })
        const result = res.data.alipay_trade_query_response
        // code === 10000 代表支付成功
        if(result.code === "10000"){
            // 更新订单支付状态
            const res = await updateOrder(ctx.request.body.out_trade_no,{status:1})
            // 更新项目 已筹集金额
            if(res){
                const {money,cat_id} = ctx.request.body
                const res1 = await updateCategory(cat_id, {amount_raised:money})
                // 更新用户 账户余额度
                const {user_id} = await findCategoryInfo(cat_id)
                const res2 = await updateUserMoney(money,user_id)
                if(res2 && res1){
                    ctx.body = {
                        ...sucOperation,
                    };
                }else{
                    ctx.body = {
                        ...failOperation
                    };
                }
            }else{
                ctx.body = {
                    ...failOperation
                };
            }
        }else{
            ctx.body = {
                ...failOperation
            };
        }

    }
}

module.exports = new OrderController()
